//////////////////////////////////////////////////////////////////////////
#include <RocketLua/RocketLua.hpp>
#include <cmath>


using namespace Rocket::Core::Lua;

//////////////////////////////////////////////////////////////////////////

Vector2f::Vector2f()
:	mX( 0 )
,	mY( 0 )
{
}


Vector2f::Vector2f( float x, float y )
:	mX( x )
,	mY( y )
{
}


float Vector2f::X() const
{
	return mX;
}


void Vector2f::X( float x )
{
	mX = x;
}


float Vector2f::Y() const
{
	return mY;
}


void Vector2f::Y( float y )
{
	mY = y;
}


Vector2f& Vector2f::Set( float x, float y )
{
	mX	=	x;
	mY	=	y;
	return *this;
}


bool Vector2f::operator == ( const Vector2f& rhs ) const
{
	return mX==rhs.mX && mY==rhs.mY;
}


Vector2f Vector2f::Negate() const
{
	return Vector2f( -mX, -mY );
}

Vector2f Vector2f::operator +( const Vector2f& rhs ) const
{
	return Vector2f( mX+rhs.mX, mY+rhs.mY );
}


Vector2f Vector2f::operator -( const Vector2f& rhs ) const
{
	return Vector2f( mX-rhs.mX, mY-rhs.mY );
}


Vector2f Vector2f::operator *( float rhs ) const
{
	return Vector2f( mX*rhs, mY*rhs );
}


Vector2f Vector2f::operator /( float rhs ) const
{
	return Vector2f( mX/rhs, mY/rhs );
}


float Vector2f::Magnitude() const
{
	return std::sqrt( float( mX*mX + mY*mY ) );
}


void Vector2f::Register( lua_State* state )
{
	luabridge::getGlobalNamespace( state )
		.beginClass< Vector2f >( "Vector2f" )
		.addConstructor< void (*)( void ) >()
		.addProperty( "x", &Vector2f::X, &Vector2f::X )
		.addProperty( "y", &Vector2f::Y, &Vector2f::Y )
		.addFunction( "Set", &Vector2f::Set )
		.addFunction( "__eq", &Vector2f::operator == )
		.addFunction( "__unm", &Vector2f::Negate )
		.addFunction( "__add", &Vector2f::operator + )
		.addFunction( "__sub", &Vector2f::operator - )
		.addFunction( "__mul", &Vector2f::operator * )
		.addFunction( "__div", &Vector2f::operator / )
		.addFunction( "Magnitude", &Vector2f::Magnitude )
		.endClass();
}
